package edu.odu.cs.sstore.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.odu.cs.sstore.domain.Document;
import edu.odu.cs.sstore.domain.User;
import edu.odu.cs.sstore.dto.CourseDTO;
import edu.odu.cs.sstore.dto.DocumentDTO;
import edu.odu.cs.sstore.dto.UserDTO;
import edu.odu.cs.sstore.repository.CourseRepository;
import edu.odu.cs.sstore.repository.DocumentRepository;
import edu.odu.cs.sstore.repository.UserRepository;

@Service(value="documentService")
public class DocumentServiceImpl implements DocumentService {

	@Autowired
	private DocumentRepository documentRepository;
	
	@Autowired
	private UserRepository userRepository;
	
	@Autowired
	private CourseRepository courseRepository;
	
	@Autowired
	private UserContextService userCtx;

	@Transactional(readOnly=true)
	public DocumentDTO getDocument(Long id) {
		return this.documentRepository.findById(id).toDTO();
	}

	@Transactional(readOnly=true)
	public List<DocumentDTO> getMyDocuments() {
		User currentUser = userCtx.getCurrentUser();
		List<DocumentDTO> dtos = new ArrayList<DocumentDTO>();
		if(currentUser != null)
			for(Document document : currentUser.getDocuments())
				if(document.getCreator().equals(currentUser))
					dtos.add(document.toDTO());
		return dtos;
	}
	
	@Transactional(readOnly=true)
	public List<DocumentDTO> getSharedDocuments() {
		User currentUser = userCtx.getCurrentUser();
		List<DocumentDTO> dtos = new ArrayList<DocumentDTO>();
		if(currentUser != null)
			for(Document document : currentUser.getDocuments())
				if(!document.getCreator().equals(currentUser))
					dtos.add(document.toDTO());
		return dtos;
	}

	public DocumentDTO createDocument(DocumentDTO document) {
		// TODO Auto-generated method stub
		return null;
	}

	public DocumentDTO updateDocument(DocumentDTO document) {
		// TODO Auto-generated method stub
		return null;
	}

	public void shareDocument(DocumentDTO document, UserDTO user) {
		// TODO Auto-generated method stub
		
	}

	public void shareDocument(DocumentDTO document, CourseDTO course) {
		// TODO Auto-generated method stub
		
	}

	public void deleteDocument(Long id) {
		// TODO Auto-generated method stub
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
//	@Transactional(readOnly=true)
//	public Document getDocument(Long id) {
//		return this.documentRepository.findById(id);
//	}
//
//	// TODO be sure this method is only accessible by ROLE_FACULTY
//	@Transactional
//	public Document createOrModifyDocument(Document document) {
//		document = this.documentRepository.saveOrUpdate(document);
//		// TODO - attach it to the current user
//		return document;
//	}
//
//	@Transactional
//	public void shareDocument(Document document, User student) {
//		student.addDocument(document);
//		userRepository.saveOrUpdate(student);
//	}
//
//	@Transactional
//	public void shareDocument(Document document, Course course) {
//		course.addDocument(document);
//		courseRepository.saveOrUpdate(course);
//	}
//
//	@Transactional
//	public void deleteDocument(Document document) {
//		this.documentRepository.deleteDocument(document);
//	}
	
}
